package com.ease.common.vo.editor;

import java.util.Enumeration;
import java.util.Properties;

import org.springframework.util.StringUtils;

import com.ease.common.vo.BundleManifest;
import com.ease.common.vo.ProjectBundle;

/**
 * 
 * @author nathanleewei
 * 
 */
public class BundleManifestEditor extends ManifestEditor<BundleManifest> {

	public BundleManifest editAsText(String text) throws IllegalArgumentException {
		Properties props = this.setAsProperties(text);

		if (null != props) {
			BundleManifest pluginManifest = new BundleManifest();
			setValue(pluginManifest);
			Enumeration<?> propNames = props.propertyNames();
			while (propNames.hasMoreElements()) {
				String name = (String) propNames.nextElement();
				String value = props.getProperty(name);
				if (BundleManifest.RequireBundle.equals(name)) {
					String[] values = StringUtils.commaDelimitedListToStringArray(value);
					if (null != values) {
						for (String v : values) {
							String[] vs = StringUtils.delimitedListToStringArray(v, ";");
							if (null != vs && vs.length > 0) {
								pluginManifest.addRequireBundle(new ProjectBundle(vs[0]));
							}
						}
					}
				} else {
					pluginManifest.put(name, value);
				}

				// Convert value to a transaction attribute.
				// tae.setAsText(value);
				// TransactionAttribute attr = (TransactionAttribute)
				// tae.getValue();
				// Register name and attribute.
				// source.addTransactionalMethod(name, attr);
				// OutputUtil.p(name, value);
			}
			return pluginManifest;
		}
		return null;
	}

}
